package expenager.project;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import android.app.Activity;
import android.app.Dialog;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.TextView;


public class ExpenagerActivity extends Activity {
    
	/** Called when the activity is first created. */
    
	Button btnPreferences, btnTransactions, btnActivities, btnStatistics, btnQuickStats, btnAutoFill, btnReminder;
	TextView textViewDate,test;
	Calendar calendar;
	DBAdapter db = new DBAdapter(this);
	DBHistory dbHistory = new DBHistory(this); 
	    
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    
        //get current date from Android Calendar
        setDate();
    
        
        btnPreferences = (Button) findViewById(R.id.btnPreferences);
        btnPreferences.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				//change to Preferences page
				Intent i = new Intent(getApplicationContext(),Preferences.class);
				startActivity(i);
			}
		});
        
        btnTransactions = (Button) findViewById(R.id.btnTransactions);
        btnTransactions.setOnClickListener(new View.OnClickListener() {
			public void onClick(View arg0) {
				//change to Transactions page
				Intent i = new Intent(getApplicationContext(),Transactions.class);
				startActivity(i);
			}
		});
        
        btnActivities = (Button) findViewById(R.id.btnActivities);
        btnActivities.setOnClickListener(new View.OnClickListener() {
			
			public void onClick(View v) {
				//change to Activities page
				Intent i = new Intent(getApplicationContext(),Activities.class);
				startActivity(i);
			}
		});
        
        btnStatistics = (Button) findViewById(R.id.btnStatistics);
        btnStatistics.setOnClickListener(new View.OnClickListener() {
			
			public void onClick(View v) {
				//change to Activities page
				Intent i = new Intent(getApplicationContext(),Statistics.class);
				startActivity(i);
			}
		});
        
        btnQuickStats = (Button) findViewById(R.id.btnQuickStat);
        btnQuickStats.setOnClickListener(new View.OnClickListener() {
			
			public void onClick(View arg0) {
				//generate QuickStats Alert Dialog
				quickStatsDialog();
			}
		});
        
        btnAutoFill = (Button) findViewById(R.id.btnAutoFill);
        btnAutoFill.setOnClickListener(new View.OnClickListener() {
			
			public void onClick(View v) {
				//change to AutoFill page
				Intent i = new Intent(getApplicationContext(),AutoFill.class);
				startActivity(i);
			}
		});
        
        btnReminder = (Button) findViewById(R.id.btnReminder);
        btnReminder.setOnClickListener(new View.OnClickListener() {
			
			public void onClick(View arg0) {
				//change to Reminder page
				Intent i = new Intent(getApplicationContext(),Lock.class);
				startActivity(i);
			}
			
		});
    }	
	
	public void setDate() {
		calendar = Calendar.getInstance();
		
		//fetch day from calendar
		SimpleDateFormat day_num = new SimpleDateFormat("EEE");
		String day = day_num.format(calendar.getTime());
		
		//fetch date from calendar
		String date = Integer.toString(calendar.get(Calendar.DATE));
		
		//fetch month from calendar
		SimpleDateFormat month_num = new SimpleDateFormat("MMM");
		String month = month_num.format(calendar.getTime());
		
		//fetch year from calendar
		String year = Integer.toString(calendar.get(Calendar.YEAR));
		
		//display current date
		textViewDate = (TextView) findViewById(R.id.textViewDate);
		textViewDate.setText(day+", "+date+" "+month+" "+year);
	}
	
	public void quickStatsDialog() {
		final Dialog dialog = new Dialog(this);
		dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
		dialog.setContentView(R.layout.quickstats);
		dialog.setCancelable(true);
		
		//back to main screen
		Button btnQuickStatOk = (Button) dialog.findViewById(R.id.btnQuickStatOk);
		btnQuickStatOk.setOnClickListener(new View.OnClickListener() {
			
			public void onClick(View v) {
				dialog.cancel();
			}
		});
		
		//get calculations of incomes
		List<Double> listIncomeAmount = new ArrayList<Double>();
		Double totalIncome = 0.00;
		
		dbHistory.open();
		Cursor c = dbHistory.getIncome();
		for(int i=0;i<c.getCount();i++) {
			listIncomeAmount.add(Double.parseDouble(c.getString(0)));
			
			if(i!=(c.getCount()-1)) {
				c.moveToNext();
				}
			}
		dbHistory.close();
		
		for(int i=0;i<listIncomeAmount.size();i++) {
			totalIncome = totalIncome + listIncomeAmount.get(i);
		}
		//...end of calculation of incomes
		
		//get calculations of expenses
		List<Double> listExpensesAmount = new ArrayList<Double>();
		Double totalExpense = 0.00;
		
		dbHistory.open();
		Cursor c2 = dbHistory.getExpense();
		for(int i=0;i<c2.getCount();i++) {
			listExpensesAmount.add(Double.parseDouble(c2.getString(0)));
			
			if(i!=(c2.getCount()-1)) {
				c2.moveToNext();
				}
			}
		dbHistory.close();
		
		for(int i=0;i<listExpensesAmount.size();i++) {
			totalExpense = totalExpense + listExpensesAmount.get(i);
		}
		//...end of calculation of outcomes
		
		//calculate balance
		Double balance = 0.00;
		balance = totalIncome - totalExpense;
		//...end of calculation of balance
		
		//update total incomes text
		TextView textIncomeAmount = (TextView) dialog.findViewById(R.id.textViewQuickIncomeAmount);
		textIncomeAmount.setText(totalIncome.toString());
		//update total expenses text
		TextView textExpenseAmount = (TextView) dialog.findViewById(R.id.textViewQuickExpenseAmount);
		textExpenseAmount.setText(totalExpense.toString());
		//update balance amount text
		TextView textBalanceAmount = (TextView) dialog.findViewById(R.id.textViewQuickBalanceAmount);
		textBalanceAmount.setText(balance.toString());
		
	dialog.show();
	}

}
   
	        